package com.example.springboot.pojo.vo;

import com.example.springboot.enums.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @author: wyb
 * @date: 2021/11/1 15:38
 * @describe:
 * @vision 1.0
 */
@Data
@ApiModel("返回信息数据模型")
public class GlobalResultVo<T> implements Serializable {

    @ApiModelProperty(value = "状态码(200：成功，其它：失败)")
    private Integer code;

    @ApiModelProperty(value = "执行信息")
    private String msg;

    @ApiModelProperty(value = "返回数据")
    private T data;

    @ApiModelProperty("总数量")
    private Integer totalCount = 0;

    @ApiModelProperty(value = "异常信息")
    private String exception;

    public static <T> GlobalResultVo<T> success(T data){
        GlobalResultVo globalResult = new GlobalResultVo();
        globalResult.setCode(ResultCode.OK.getCode());
        globalResult.setMsg(ResultCode.OK.getDesc());
        globalResult.setData(data);
        return globalResult;
    }

    public static <T> GlobalResultVo<T> success(T data, Integer totalCount){
        GlobalResultVo globalResult = new GlobalResultVo();
        globalResult.setCode(ResultCode.OK.getCode());
        globalResult.setMsg(ResultCode.OK.getDesc());
        globalResult.setData(data);
        globalResult.setTotalCount(totalCount);
        return globalResult;
    }

    public static <T> GlobalResultVo<T> error(String msg){
        GlobalResultVo globalResult = new GlobalResultVo();
        globalResult.setCode(ResultCode.FAILURE.getCode());
        globalResult.setMsg(msg);
        return globalResult;
    }

    public static GlobalResultVo error(Integer code, String msg, String exception){
        GlobalResultVo globalResult = new GlobalResultVo();
        globalResult.setCode(code);
        globalResult.setMsg(msg);
        globalResult.setException(exception);
        return globalResult;
    }

    public static GlobalResultVo error(String msg, String exception){
        GlobalResultVo globalResult = new GlobalResultVo();
        globalResult.setMsg(msg);
        globalResult.setException(exception);
        return globalResult;
    }
}
